//
//  DestinationViewController.m
//  EDPS
//
//  Created by 张维扬 on 15/11/23.
//  Copyright © 2015年 张维扬. All rights reserved.
//

#import "DestinationViewController.h"
#import "DestinationFirCollectionViewCell.h"
#import "DestinationDetailsViewController.h"
#import "UIImageView+WebCache.h"
#import "ZWY_NetHandle.h"
#import "BaseModel.h"
#import "elements.h"
#import "data.h"

@interface DestinationViewController ()<UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) UICollectionView *collectionView;

// elements层数据model
@property (nonatomic, retain) NSMutableArray *elementsArr;

// data层数据
@property (nonatomic, retain) NSMutableArray *twmpArr;

@end

@implementation DestinationViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"目的地";
    [self creatCollectionView];
    [self getData];
}
- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:YES];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 获取数据
- (void)getData
{
    self.elementsArr = [NSMutableArray array];
    self.twmpArr = [NSMutableArray array];
    NSString *url = @"http://api.breadtrip.com/destination/v3/";
    [ZWY_NetHandle getURL:url Cookie:nil parameter:nil result:^(id result) {
        self.elementsArr = [elements transformWithArray:result[@"elements"]];
        self.twmpArr = result[@"elements"];
        [self.collectionView reloadData];
    }];
}
#pragma mark - 创建collectionView
- (void)creatCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake((ScreenWidth / 2 - 20), 150);
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 10, 15);
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight) collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    [self.collectionView registerClass:[DestinationFirCollectionViewCell class] forCellWithReuseIdentifier:@"twoCell"];
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.collectionView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.collectionView];
}
#pragma mark - collectionView方法
#pragma mark - 点击方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    DestinationDetailsViewController *DetailsVc = [[DestinationDetailsViewController alloc]init];
    DetailsVc.type =  self.twmpArr[indexPath.section][@"data"][indexPath.row][@"type"];
    DetailsVc.numId = self.twmpArr[indexPath.section][@"data"][indexPath.row][@"id"];
    DetailsVc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:DetailsVc animated:YES];
}
#pragma mark - cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    NSArray *temp = [[self.elementsArr[section] data] mutableCopy];
    return temp.count;
}
#pragma mark - cell内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    DestinationFirCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"twoCell" forIndexPath:indexPath];
    [cell.cover_s sd_setImageWithURL:[NSURL URLWithString:self.twmpArr[indexPath.section][@"data"][indexPath.row][@"cover_s"]] placeholderImage:[UIImage imageNamed:@"hold"]];
    cell.name.text = self.twmpArr[indexPath.section][@"data"][indexPath.row][@"name"];
    return cell;
}
#pragma mark - 分区个数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.elementsArr.count;
}
#pragma mark - 分区大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGSize size={320,40};
    return size;
}
#pragma mark - 分区名
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (kind == UICollectionElementKindSectionHeader)
    {
        UICollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        UILabel *tem = [[UILabel alloc]initWithFrame:CGRectMake(20, 6, ScreenWidth - 40, ScreenWidth * 0.08)];
        tem.backgroundColor = [UIColor whiteColor];
        
        tem.text = [self.elementsArr[indexPath.section] title];
        
        [headerView addSubview:tem];
        
        return headerView;
    }
    return nil;
}
@end